package com.qf.springmvc02.controller;

import com.github.pagehelper.PageInfo;
import com.qf.springmvc02.entity.User;
import com.qf.springmvc02.response.Response;
import com.qf.springmvc02.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author nk
 * @date 2022/5/13 14:51
 * 获取用户列表/根据ID查看用户/编辑/删除
 */

@RestController
@RequestMapping("/vue")
public class VueUserController {
    @Autowired
    UserService userService;

    @RequestMapping("/users")
    public Response<PageInfo<User>> getAllUser(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
                                               @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        PageInfo<User> pageInfo = userService.selectAllUser(pageIndex, pageSize);
        return Response.success(pageInfo);
    }

    //根据ID删除用户
    @GetMapping("/users/{id}")
    public Response<User> deleteById(@PathVariable("id") Integer id){
        userService.deleteById(id);
        return Response.success();
    }


    //查询单个用户
    @GetMapping("/getUser/{id}")
    public Response<User> selectById(@PathVariable("id") Integer id){
        return Response.success(userService.selectById(id));
    }

    //编辑对象   响应对象
    @PostMapping("/user")
    public  Response<Integer> updateUser(User user){
        return Response.success(userService.update(user));
    }

    //新增对象
    @PostMapping("/insertUser")
    public Response<Integer> insertUser(User user){
        return Response.success(userService.addUser(user));
    }
}
